package edu.xust.example;





import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.xust.entity.Student;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class TestJson_zjj {
   public static void main(String[] args) throws IOException {
       String jsonstring1 = "[{\"sno\":\"0001\",\"sex\":\"男\"},{\"sno\":\"0002\",\"sex\":\"女\"}]";
       Student s1 = new Student("1", "男");
       Student s2 = new Student("2", "女");

       ObjectMapper objectMapper = new ObjectMapper();

       List<Student> students = Arrays.asList(new Student[]{s1, s2});
       String s = objectMapper.writeValueAsString(students);
       System.out.println(s);
       System.out.println();
       String student = " {\"sno\":\"0001\",\"sex\":\"男\"}";
       //json字符串转换为java对象
       Student student1 = objectMapper.readValue(student, Student.class);
       System.out.println(student1);
       System.out.println();


       //json字符串转换为java集合对象，集合对象需要使用引用类型
       List<Student> students1 = objectMapper.readValue(jsonstring1, new TypeReference<List<Student>>() {
       });
       students1.forEach(System.out::println);
   }
}
